package com.bds.wm.service;

import com.alibaba.fastjson.JSONObject;
import com.bds.wm.model.procmodel.Competitor;
import com.bds.wm.utils.FormatUils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.log4j.Logger;
import org.nutz.dao.Sqls;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by DongChenchen on 2018/7/17 0017
 */
@Service
public class CompetitorViewService {
    private Logger LOGGER = Logger.getLogger(CityViewService.class);
    @Resource
    private NutDao dao;
    public List<Competitor> getComparisonbyProduct(String city, Integer start, Integer end,String brand) {
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getComparisonbyProduct @StartMonth ,@EndMonth ,@City ,@Brand");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(Competitor.class));
        sql.params().set("City", city); //设置入参
        sql.params().set("StartMonth", start); //设置入参
        sql.params().set("EndMonth", end); //设置入参
        sql.params().set("Brand", brand); //设置入参
        dao.execute(sql);
        return sql.getList(Competitor.class);
    }
    /**
     * 拼接前台模型
     * @param cates
     * @return
     */
    public JSONObject groupByCategory(List<Competitor> cates){
        JSONObject json=new JSONObject();
        //父表 [[],[],[]...]  汇总字段：Monthly Volume，Monthly Value，SR
        Map<String, List<String>> father = Maps.newLinkedHashMap();
        //子表 {key1:[[],[],[]...],key2:[[],[],[]...]}  汇总字段：Monthly Volume，Monthly Value，SR
        Map<String, List<Object>> son = Maps.newLinkedHashMap();
        cates.forEach(cate->{
            try {
                //父表
                String product = "品类-"+cate.getSubproducttype();
                if(!father.containsKey(product)){
                    List<String> cate1= Lists.newArrayList(
                            product,
                            "",
                            "",
                            FormatUils.formatConvmax(cate.getSubTC()),//Monthly Volume，
                            FormatUils.formatConvmax(cate.getSubSalesValue()),//Monthly Value，
                            "",
                            FormatUils.formatConv(cate.getSubSR()));//SR
                    father.put(product,cate1);
                }
                List<String> subCate1= Lists.newArrayList(
                        cate.getProductName(),
                        "",
                        FormatUils.formatConv(cate.getProductPrice()),
                        FormatUils.formatConvmax(cate.getProductTC()),
                        FormatUils.formatConvmax(cate.getProductSalesValue()),
                        FormatUils.formatConv(cate.getMenuMix()),
                        FormatUils.formatConv(cate.getSR()));
                if(son.containsKey(product)){
                    List<Object> subs = son.get(product);
                    subs.add(subCate1);
                    son.put(product,subs);
                }else{
                    List<Object> subs = Lists.newArrayList();
                        subs.add(subCate1);
                    son.put(product,subs);
                }
            }catch (Exception e){
                LOGGER.warn("[service]-[33]-groupByCategory获取竞品信息异常", e);
            }
        });
        json.put("cate",father.values());
        json.put("subCate",son);
        return json;
    }
}
